using UnityEngine;
using QFramework;
using System.Collections.Generic;

using System;

// 1.请在菜单 编辑器扩展/Namespace Settings 里设置命名空间
// 2.命名空间更改后，生成代码之后，需要把逻辑代码文件（非 Designer）的命名空间手动更改
namespace QFramework.Example
{
	public partial class EnemyInsManager : InstanceFather<EnemyInsManager>
	{

        

		public EnemyConfiguration enemyDatas;

        [NonSerialized]
		public EnemyDatas enemyData = null;

		public Queue<EnemyDatas> enemies = new Queue<EnemyDatas>();

        private float instime = 0;
        private float waveTime = 0;
        private int waveIndex = 0;
        //public override void Awake()
        //{
        //    if (enemyDatas.enemyDatas.Count>0)
        //    {
        //        foreach (var enemy in enemyDatas.enemyDatas)
        //        {
        //            enemies.Enqueue(enemy);
        //        }
        //    }

        //}

        protected override void Awake()
        {
            base.Awake();
            if (enemyDatas.enemyDatas.Count > 0)
            {
                foreach (var enemy in enemyDatas.enemyDatas)
                {
                    enemies.Enqueue(enemy);
                }
            }
        }




        void Start()
		{
			
		}

        private void Update()
        {
            InsEnemy();
        }


        void InsEnemy()
        {
            if (enemyData == null)
            {
                if (enemies.Count > 0)
                {
                    enemyData = enemies.Dequeue();
                    instime = 0;
                    waveTime = 0;
                    waveIndex += 1;
                }
               
            }
           

            if (enemyData != null)
            {
                
                waveTime += Time.deltaTime;
                instime += Time.deltaTime;
                if (waveTime >= enemyData.WaveTime)
                {
                    instime = 0;
                    waveTime = 0;
                    enemyData = null;
                }

                if (instime >= enemyData.InsTime)
                {
                    //Debug.Log("生成敌人");
                    ///置零
                    instime = 0;
                    ///获取player
                    var player = Player.Instance;

                    if (player!=null)
                    {
                        var xOry = RandomUtility.Choose(-1, 1);
                        var pos = Vector2.zero;
                        if (xOry == -1)
                        {
                            pos.x = RandomUtility.Choose(CameraController.Instance.LT.position.x,
                                CameraController.Instance.RT.position.x);
                            pos.y = UnityEngine.Random.Range(CameraController.Instance.LT.position.y,
                               CameraController.Instance.RT.position.y);
                        }
                        else
                        {
                            pos.y = RandomUtility.Choose(CameraController.Instance.LT.position.y,
                              CameraController.Instance.RT.position.y);
                            pos.x = UnityEngine.Random.Range(CameraController.Instance.LT.position.x,
                               CameraController.Instance.RT.position.x);
                        }

                        var enemy = PoolManager.Instance.GetObj("Enemys/"+enemyData.Enemys);

                        //var enemy = Instantiate(enemyData.Enemys);
                        enemy.transform.position = pos;
                        var enemyComponent = enemy.GetComponent<EnemyFathers>();
                        if (enemyComponent!=null)
                        {
                            enemyComponent.EnemyHp = enemyData.Hp;
                            enemyComponent.EnemyMoveSpeed = enemyData.MoveSpeed;
                            enemyComponent.SetHpScale(enemyData.HpScale);
                            enemyComponent.SetSpeedScale(enemyData.SpeedCale);
                        }

                        enemy.Show();
                    }
                    
                }
               

            }
        }

        /// <summary>
        /// 初始化Enemy数据
        /// </summary>
        /// <param name="enemys"></param>
        public void SetEnemyData(GameObject enemys)
        {
            
            var enemy = enemys.GetComponent<EnemyFathers>();
            if (enemy!=null)
            {
               
                if (enemyData != null)
                {
                    
                    enemy.EnemyHp = enemyData.HpScale;
                    enemy.EnemyMoveSpeed = enemyData.SpeedCale;
                }
                else
                {
                    Debug.Log("enemyData为空");
                }

            }
            else
            {
                Debug.Log("Enemy脚本为空");
            }
           

        }
    }
}
